﻿using JUtil.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JUtil;
using System.Reflection;
using JUtil.Web.Extensions;

public partial class _Default : System.Web.UI.Page 
{
    //IDbAllOp db;
    //DbUtils DbUtils;

    protected _Default()
    {
        //db = new XDatabase(DbProvider.OleDb, @"Provider=SQLOLEDB.1;Password=sapass;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=JELLY\SQLEXPRESS");

        //DbUtils = new DbUtils(db);
    }

    private object GetLatestData(string field)
    {
        return null;
    }

    private double WEIGHT = 75.0;
    private double WAISTLINE = 85.0;
    private double RUNNING_MILEAGE = 0.0;
    private double PRESS_UP_TIME = 0.0;
    private double SITUP_TIME = 0.0;
    private double SMOKED_CIGARETTES_NUMBER = 0.0;
    private DateTime SLEEP_TIME = DateTime.Now;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            tfDATE.Text = DateTime.Now.ToString("yyyy/MM/dd");
            tfWEIGHT.Text = WEIGHT.ToString();
            tfWAISTLINE.Text = WAISTLINE.ToString();
            tfRUNNING_MILEAGE.Text = RUNNING_MILEAGE.ToString();
            tfPRESS_UP_TIME.Text = PRESS_UP_TIME.ToString();

            tfSITUP_TIME.Text = SITUP_TIME.ToString();
            tfSMOKED_CIGARETTES_NUMBER.Text = SMOKED_CIGARETTES_NUMBER.ToString();

            //tfSLEEP_TIME.Text = "04:00am";
            int Hour = SLEEP_TIME.Hour;
            int Minute = SLEEP_TIME.Minute;
            string AmPm = (0 <= Hour && Hour <= 11) ? "am" : "pm";
            string format = string.Format("{0}:{1}{2}", Hour.ToString("00"), Minute, AmPm);
            tfSLEEP_TIME.Text = format;
        }
    }

    private DailyHealthRecord GetSubmitData()
    {
        DailyHealthRecord data = new DailyHealthRecord();

        data.DATE = DateTime.Now.ToDateBegin();
        data.WEIGHT = Convert.ToDouble(tfWEIGHT.Text);
        data.WAISTLINE = Convert.ToDouble(tfWAISTLINE.Text);
        data.RUNNING_MILEAGE = Convert.ToDouble(tfRUNNING_MILEAGE.Text);
        data.PRESS_UP_TIME = Convert.ToInt32(tfPRESS_UP_TIME.Text);

        data.SITUP_TIME = Convert.ToInt32(tfSITUP_TIME.Text);
        data.SMOKED_CIGARETTES_NUMBER = Convert.ToInt32(tfSMOKED_CIGARETTES_NUMBER.Text);

        int Hours = Convert.ToInt32(tfSLEEP_TIME.Text.Substring(0, 2));
        int Minutes = Convert.ToInt32(tfSLEEP_TIME.Text.Substring(3, 2));
        data.SLEEP_TIME = DateTime.Now.ToDateBegin().AddHours(Hours).AddMinutes(Minutes);

        data.HEAD_SHOT = GetPostFile();

        return data;
    }

    private byte[] GetPostFile()
    {
        // PhotoUpload
        System.IO.Stream fs = PhotoUpload.PostedFile.InputStream;
        byte[] data = new byte[fs.Length];
        fs.Read(data, 0, (int)fs.Length);
        fs.Close();
        return data;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            //string insertSql = DbUtils.GetInsertSql("DailyHealthRecord");

            //insertSql = Formatter.GenericFormat<DailyHealthRecord>(DatabaseType.SqlServer, insertSql, GetSubmitData(), null);

            //db.ExecuteSQL(insertSql);
        }
        catch (Exception ex)
        {
        	Log.ReportError(ex);
        }
    }

}
